package com.cheval.rpcframe.protocal;

import com.cheval.rpcframe.common.Message;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class ConsumerClient {

    public Object removeInvoke(String hostname, int port, Message message) {
        try {
            URL url = new URL("http://" + hostname + ":" + port);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            OutputStream outputStream = connection.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(message);
            oos.flush();
            oos.close();

            InputStream inputStream = connection.getInputStream();

            ObjectInputStream ois = new ObjectInputStream(inputStream);
            Object o = ois.readObject();
            inputStream.close();
            ois.close();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
